<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="title">后台管理系统</h2>
      <el-form :model="loginForm" >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin" :loading="loading">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {useUserStore} from '@/stores/user'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const userStore = useUserStore()

const loginForm = reactive({
  username: '',
  password: ''
})

const handleLogin = async () => {
  
  try {
    loading.value = true
    
    // 调用登录接口
    await userStore.loginAction(loginForm)

    console.log(loginForm,'index.vue==>');
    
    // 登录成功，跳转到之前的页面或首页
    const redirect = route.query.redirect || '/'
    router.push(redirect)
    
    ElMessage.success('登录成功')
  } catch (error) {
    ElMessage.error(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;
  
  .login-card {
    width: 400px;
    padding: 20px;
    
    .title {
      text-align: center;
      margin-bottom: 30px;
      color: #409EFF;
    }
    
    .login-button {
      width: 100%;
    }
  }
}
</style> 